<#assign title='安全管理'>
<#include 'header.ftl'>

<div class="layui-card">
  <div class="layui-card-body layui-row">
    <form style="padding-top: 5px;" class="layui-form" lay-filter="safe-form">
      <div class="layui-col-xs-offset3 layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label">旧密码</label>
          <div class="layui-input-block">
            <input name="oldPass" lay-verType="tips" lay-verify="required" type="password"
                   placeholder="请输入旧密码" class="layui-input">
          </div>
        </div>
      </div>

      <div class="layui-col-xs-offset3 layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label">新密码</label>
          <div class="layui-input-block">
            <input name="newPass" lay-verType="tips" lay-verify="required|pass" type="password"
                   placeholder="请输入新密码" class="layui-input">
          </div>
        </div>
      </div>

      <div class="layui-col-xs-offset3 layui-col-xs6">
        <div class="layui-form-item">
          <label class="layui-form-label">确定密码</label>
          <div class="layui-input-block">
            <input name="twoPass" lay-verType="tips" lay-verify="required|same" type="password"
                   placeholder="请再次输入新密码" class="layui-input">
          </div>
        </div>
      </div>

      <div class="layui-col-xs-offset3 layui-col-xs6">
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn layui-btn-fluid" lay-submit>确定修改</button>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>
<script src="${root}/static/js/common.js"></script>
<script>
  var index = 0;
  layui.use(['form', 'jquery', 'layer'], function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var $pass = $('[name="newPass"]');
    layer.config({
      end: function () {
        if (index > 0) index = 0;
      }
    });

    form.on('submit', function (obj) {
      if (index > 0) {
        layer.close(index);
        index = 0;
        return false;
      }
      var fd = obj.field;
      $.post('${root}/user/safe', fd, function (data) {
        if (data.status == 200) {
          index = layer.msg("修改成功！", {icon: 6, time: 1200});
          window.setTimeout(function () {
            location.href = '${root}/login'
          }, 1200);
        } else {
          index = layer.alert('失败：' + data.message, {title: '失败提示', icon: 5});
        }
      });
      return false;
    });

    form.verify({
      same: function (val, elem) {
        if (val !== $pass.val()) {
          return "两次密码不相同！";
        }
      },
      pass: function (val, elem) {
        if (val.length < 6) {
          return "密码别小于6位哦！";
        }
      }
    })
  });
</script>

<#include 'footer.ftl'>
